<?php 

// Define the contant for paganation
define('page_size', 3); 

class Pager
{
	var $table_name;
	var $current_page;
	var $row_count;
	var $page_offset;
	var $page_size = 0;
	var $page_navigation;

	function __construct( $args )
	{
		$this->table_name = $args['table_name'];
		$this->page_size = $args['page_size'];
		if($args['current_page']<=1 || $args['current_page']=='')
			$this->current_page = 1;
		else 
			$this->current_page = $args['current_page'];
		$this->get_row_count();
		$this->calc_page_offset();
	}

	function __destruct()
	{
		return true;
	}
		
	function calc_page_offset()
	{
		if($this->current_page<=1 || $this->current_page=='') 
			$this->page_offset = 0;
		else
			$this->page_offset = ($this->current_page - 1) * $this->page_size;	
	}
	
	function get_page_size()
	{
		return $this->page_size;
	}
	
	function get_row_count()
	{
		global $wpdb;
		$this->row_count = $wpdb->get_var($wpdb->prepare("SELECT COUNT(*) FROM $this->table_name;"));
		return $this->row_count;
	}

	function display()
	{
		$page_count = ceil($this->row_count/$this->page_size);	
		$pre_page = ($this->current_page == 1)? 1 : $this->current_page - 1;
		$next_page= ($this->current_page == $page_count)? $page_count : $this->current_page + 1 ;

		$this->page_navigation .= "第 $this->current_page/$page_count 页 共 $this->row_count 条记录 ";
		$this->page_navigation .= "<a href='?page=1'>首页</a> ";
		$this->page_navigation .= "<a href='?page=$pre_page'>前一页</a> ";
		$this->page_navigation .= "<a href='?page=$next_page'>后一页</a> ";
		$this->page_navigation .= "<a href='?page=$page_count'>末页</a> ";
// 		$this->page_navigation .= "跳到<select name='to11page' size='1' onchange='window.location=\"?page=\"+this.value'>\n";

// 		for($i=1;$i<=$page_count;$i++)
// 		{
// 			if($i==$this->current_page)
// 				$this->page_navigation.="<option value='$i' selected>$i</option>\n";
// 			else
// 				$this->page_navigation.="<option value='$i'>$i</option>\n";
// 		}
		
		echo $this->page_navigation;
	}
}
?>